package com.etc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.entity.Announcement;
import com.etc.entity.User;
import com.etc.service.impl.AnnouncementServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class AnnouncementController
 */
@WebServlet("/AnnouncementController")
@MultipartConfig
public class AnnouncementController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	AnnouncementServiceImpl service = new AnnouncementServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AnnouncementController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取参数 参数名op的值
		String op = request.getParameter("op");
		//提升:加入session的判断:从session中获取用户登录的信息,如果有,才能执行查询,添加等等的操作
		HttpSession session = request.getSession();
		Object object = session.getAttribute("frontUser");
		Object object2 = session.getAttribute("backUser");
		if (object2 != null) {
			if("queryAnn".equals(op)) {
				//用户留言全查
				queryAnn(op, request, response,session);
				
			}else if("queryAnnDetail".equals(op)) {
				//用户留言详情查询
				queryAnnDetail(op, request, response,session);
				
			}else if("queryAnnByPage_ajax".equals(op)) {
				//用户留言分页查询
				queryAnnByPage_ajax(op, request, response,session);
				
			}else if("delAnn_ajax".equals(op)) {
				//删除指定的用户留言
				delAnn_ajax(op, request, response);
			}
		}else {
			
		}
		if (object != null) {
			if("showNewAnn_ajax".equals(op)) {
				showNewAnn_ajax(op, request, response,session);
			}else if("addNewAnn_ajax".equals(op)) {
				addNewAnn_ajax(object,op, request, response,session);
				
			}else if("queryByTitleLike".equals(op)) {
				//前台用户留言关键字查询
				queryByTitleLike(op, request, response,session);
			}
		} 
		
		
		
	}
	/**
	 * 前台用户留言信息（不带分页根据关键字查询）
	 * @param op
	 * @param request
	 * @param response
	 * @param session
	 * @author zww
	 * @throws IOException 
	 */
	public void queryByTitleLike(String op, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws IOException {
		//获取输入的关键字
		String tkey = request.getParameter("titlekey");
		//调用业务层
		List<Announcement> list = service.queryByTitleLike(tkey);
		//通过ajax返回数据
		Gson gson=new Gson();
		String json = gson.toJson(list);
		response.getWriter().print(json);
		response.getWriter().close();
		
	}

	/**
	 * 用户留言详情
	 * @param op
	 * @param request
	 * @param response
	 * @param session
	 * @throws IOException 
	 * @throws ServletException 
	 * @author zww
	 */
	public void queryAnnDetail(String op, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws ServletException, IOException {
		//1.获取id
		String aid = request.getParameter("aid");
		//2.调用业务层的方法
		Announcement announcement = service.queryAnnById(Integer.parseInt(aid));
		//3.将announcement保存到request对象中
		request.setAttribute("announcement", announcement);

		//2.进行页面跳转  forward转发
		request.getRequestDispatcher("back/announcementDetail.jsp").forward(request, response);
		
	}

	/**
	 * 删除指定的用户留言
	 * @param op
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @author zww
	 */
	public void delAnn_ajax(String op, HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取用户传递过来的参数articleId
		String a_id = request.getParameter("a_id");
		//调用service
		boolean flag = service.rmvAnnouncement(Integer.parseInt(a_id));
		//通过ajx返回json数据
		Gson gson = new Gson();
	    String jsonFlag = gson.toJson(flag);
	    response.getWriter().print(jsonFlag);
	    response.getWriter().close();	
	}

	/**
	 * 全查
	 * @param op
	 * @param request
	 * @param response
	 * @param session
	 * @throws IOException 
	 * @throws ServletException 
	 * @author zww
	 */
	public void queryAnn(String op, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		//调用查询方法
		List<Announcement> list = service.queryAllAnnouncement();
		//这里list对象应该渲染给jsp页面
		//1.传递数据给jsp页面,jsp页面做显示数据  传递数据:request.setAttribute("key", value);,到了下一个页面就可以通过对应的
		//request.getAttribute("key");
		request.setAttribute("list", list);//将list对象存放在request的作用域中,在下一个页面就可以获取list

		//2.进行页面跳转  forward转发
		request.getRequestDispatcher("back/announcement.jsp").forward(request, response);
		
	}

	/**
	 * 分页查询
	 * @param op
	 * @param request
	 * @param response
	 * @param session
	 * @throws IOException
	 * @author zww
	 */
	public void queryAnnByPage_ajax(String op, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {
		//1.默认的页码和显示条数
		int pageNo=1;  //默认
		int pageSize=3; //默认
		String titlekey=""; //如果没有输入 关键字。默认全查
		
		//2.如果前台有指定的值就按照前台的显示，没有的话就按照默认值处理
		if(request.getParameter("pageNo")!=null) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		if(request.getParameter("pageSize")!=null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		if(request.getParameter("titlekey")!=null) {
			titlekey = request.getParameter("titlekey");
		}
		
		//调用业务层		
		PageData pageData = service.queryAnnByLikeAndLimit(pageNo, pageSize, titlekey);
		
		//4.通过ajax返回数据
		Gson gson=new Gson();
		String json = gson.toJson(pageData);
		response.getWriter().print(json);
		response.getWriter().close();
		
		
		
	}

	private void addNewAnn_ajax(Object object, String op, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		String a_title = request.getParameter("a_title");
		String a_content = request.getParameter("a_content");
		User user = (User) object;
		//调用业务层添加数据
		boolean flag = service.addAnnouncement(new Announcement(user, a_title, a_content));
		//返回数据
		Gson gson =new Gson();
		String jsonFlag=gson.toJson(flag);
		try {
			response.getWriter().print(jsonFlag);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			response.getWriter().close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	private void showNewAnn_ajax(String op, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		//设置响应内容格式
		response.setContentType("application/json;charset=utf-8");
		//调用业务层获取数据
		List<Announcement> list = service.queryNewAnnouncement();
		Gson gson =new Gson();
		String json=gson.toJson(list);
		try {
			response.getWriter().print(json);
			response.getWriter().close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
